package com.qf.shopping.service.impl;

import com.qf.shopping.mapper.article.ArticleActionMapper;
import com.qf.shopping.mapper.article.ArticleMapper;
import com.qf.shopping.pojo.System.User;
import com.qf.shopping.pojo.article.ArticleAction;
import com.qf.shopping.pojo.article.ArticleTag;
import com.qf.shopping.pojo.message.Tag;
import com.qf.shopping.service.IArticleSericve;
import com.qf.shopping.ustils.TimeUtil;
import com.qf.shopping.vo.article.ArticleConstom;
import com.qf.shopping.vo.article.ArticleSearch;
import com.qf.shopping.vo.article.ArticleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @Author 82024
 * @Date 2019/4/16 19:17
 * @Version 1.0
 * @Description
 **/
@Service
@Transactional
public class ArticleServiceImpl implements IArticleSericve {

    @Autowired
    private ArticleMapper articleMapper = null;

//    @Autowired
//    private ArticleTagMapper articleTagMapper = null;

    @Autowired
    private ArticleActionMapper articleActionMapper = null;

//    @Autowired
//    private ArticleCategoryMapper articleCategoryMapper = null;

//    @Autowired
//    private TagMapper tagMapper = null;

    /**
     * 查询未提交与未通过的资讯（草稿箱展示与搜索）
     * 最终版
     * @param map
     * @return
     */
    @Override
    public List<ArticleVO> findSomeArticle(Map<String, Object> map) {
        List<ArticleConstom> constoms = articleMapper.findSomeArticle(map);
        return ArticleVO.getArticleVOS(constoms);
    }

    /**
     * 审核资讯（展示与搜索）
     * 最终版
     * @return
     */
    @Override
    public List<ArticleVO> auditArticle(Map<String, Object> map) {
        List<ArticleConstom> constoms = articleMapper.auditArticle(map);
        return ArticleVO.getArticleVOS(constoms);
    }

    /**
     * 发布资讯（展示与搜索）
     * @return
     * @return
     */
    @Override
    public List<ArticleVO> publishArticle(Map<String, Object> map) {
        List<ArticleConstom> constoms = articleMapper.publishArticle(map);
        return ArticleVO.getArticleVOS(constoms);
    }

    /**
     * 资讯列表（展示与搜索）
     * @return
     */
    @Override
    public List<ArticleVO> findAllArticle(ArticleSearch articleSearch) {
        return ArticleVO.getArticleVOS(articleMapper.findAllArticle(articleSearch));

    }

    /**
     * 根据ID查找资讯
     * @param id
     * @return
     */
    @Override
    public ArticleVO findArticleById(Long id) {
        ArticleVO vo = new ArticleVO();
        vo.setArticleConstom(articleMapper.findById(id));
        return vo;
    }



    /**
     * 添加资讯
     * @param articleVO
     * @param user
     */
    @Override
    public void addArticle(ArticleVO articleVO, User user) {
        Random random = new Random();
        StringBuilder tags = new StringBuilder();
        tags = tags.append("");
        //生成资讯ID
//        Long articleId = random.nextLong();

        //生成创建时间
        Long time = TimeUtil.getTime();


        //3.添加资讯
        ArticleConstom constom = articleVO.getArticleConstom();
        constom.setState(0);
        constom.setStateOrder(0);
        constom.setPublishState(0);
        constom.setTags(tags.toString());
        constom.setCreateTime(time);
        constom.setCreateUid(user.getId());
        constom.setCreateUname(user.getUserName());
        constom.setLastUpdateTime(TimeUtil.getTime());
        constom.setLastUpdateUid(user.getId());
        constom.setLastUpdateUname(user.getUserName());
        articleMapper.addArticle(constom);

        //资讯关联的标签
        List<Tag> tags1 = constom.getTags1();
        if (tags1 != null && tags1.size() > 0){
            //1.为资讯标签中间表添加数据
            ArticleTag articleTag = new ArticleTag();
            for (Tag id : tags1) {
                Long articleTagId = random.nextLong();
                articleTag.setId(articleTagId);
                articleTag.setArticleId(articleVO.getArticleConstom().getId());
                articleTag.setTagId(id.getId());
            }
            //2.拼接标签名字
//            List<String> tagNames = tagMapper.findTagNameByIds(tagIds);
//            for (int i = 0; i < tagNames.size(); i++) {
//                if (i != 0){
//                    tags.append(",");
//                }
//                tags.append(tagNames.get(i));
//            }
        }

        //4.添加资讯操作记录
        ArticleAction articleAction = new ArticleAction();
        articleAction.setArticleId(articleVO.getArticleConstom().getId());
        articleAction.setOperatorUid(user.getId());
        articleAction.setOperatorUserName(user.getUserName());
        articleAction.setCreateTime(TimeUtil.getTime());
        articleAction.setContent("创建了一条资讯");
        articleAction.setOperatorType(1);
        articleActionMapper.addArticleAction(articleAction);

    }

    /**
     * 编辑资讯
     * @param articleVO
     * @param user
     */
    @Override
    public void editArticle(ArticleVO articleVO, User user) {
        //1.设置资讯的最后修改人信息
        ArticleConstom constom = articleVO.getArticleConstom();
        ArticleConstom ac = articleMapper.findById(constom.getId());
        ac.setTitle(constom.getTitle());
        ac.setSource(constom.getSource());
        ac.setSummary(constom.getSummary());
        ac.setAuthor(constom.getAuthor());
        ac.setAllowUp(constom.getAllowUp()==null?0:1);
        ac.setAllowFav(constom.getAllowFav()==null?0:1);
        ac.setContent(constom.getContent());
//        ac.setTags(constom.getTags());  //这个有问题，应该发Ajax来修改标签
        ac.setContent(constom.getContent());
        ac.setLastUpdateTime(TimeUtil.getTime());
        ac.setLastUpdateUid(user.getId());
        ac.setLastUpdateUname(user.getUserName());
        articleMapper.editArticle(ac);

        //2.添加资讯操作记录
        ArticleAction articleAction = new ArticleAction();
        articleAction.setArticleId(ac.getId());
        articleAction.setOperatorUid(user.getId());
        articleAction.setOperatorUserName(user.getUserName());
        articleAction.setCreateTime(TimeUtil.getTime());
        articleAction.setContent("编辑了一条资讯");
        articleAction.setOperatorType(2);
        articleActionMapper.addArticleAction(articleAction);
    }

    /**
     * 审核资讯（审核通过时使用）
     * @param id
     * @param user
     */
    @Override
    public void passArticle(Long id, User user) {
        //1.审核资讯
        ArticleConstom constom = articleMapper.findById(id);
        constom.setState(2);
        constom.setStateOrder(2);
        constom.setLastAuditTime(TimeUtil.getTime());
        constom.setLastAuditUid(user.getId());
        constom.setLastAuditUname(user.getUserName());
        articleMapper.updateArticle(constom);

        //2.添加资讯操作记录
        ArticleAction articleAction = new ArticleAction();
        articleAction.setArticleId(id);
        articleAction.setOperatorUid(user.getId());
        articleAction.setOperatorUserName(user.getUserName());
        articleAction.setCreateTime(TimeUtil.getTime());
        articleAction.setContent("审核通过了一条资讯");
        articleAction.setOperatorType(3);
        articleActionMapper.addArticleAction(articleAction);
    }

    /**
     * 驳回一条资讯
     * @param id
     * @param msg
     * @param user
     */
    @Override
    public void disableArticle(Long id, String msg, User user) {
        //1.驳回资讯
        ArticleConstom constom = articleMapper.findById(id);
        constom.setState(3);
        constom.setStateOrder(3);
        constom.setLastAuditTime(TimeUtil.getTime());
        constom.setLastAuditUid(user.getId());
        constom.setLastAuditUname(user.getUserName());
        constom.setLastRejectReason(msg);
        articleMapper.updateArticle(constom);

        //2.添加资讯操作记录
        ArticleAction articleAction = new ArticleAction();
        Random random = new Random();
        long articleActionId = random.nextLong();
        articleAction.setId(articleActionId);
        articleAction.setArticleId(id);
        articleAction.setOperatorUid(user.getId());
        articleAction.setOperatorUserName(user.getUserName());
        articleAction.setCreateTime(TimeUtil.getTime());
        articleAction.setContent("审核驳回了一条资讯");
        articleAction.setOperatorType(3);
        articleActionMapper.addArticleAction(articleAction);
    }

    @Override
    public void topArticle(Long id, User user) {
        ArticleConstom constom = articleMapper.findById(id);
        ArticleAction articleAction = new ArticleAction();

        //资讯的推荐或者取消推荐
        if(constom.getHomeTop() == 1 || constom.getChannelTop() == 1){//取消推荐
            constom.setHomeTop(0);
            constom.setChannelTop(0);
            articleMapper.updateArticle(constom);

            articleAction.setContent("取消推荐一条资讯");
        }else if (constom.getHomeTop() == 0 || constom.getChannelTop() == 0){//推荐资讯
            constom.setHomeTop(1);
            constom.setChannelTop(1);
            articleMapper.updateArticle(constom);

            articleAction.setContent("推荐一条资讯");
        }


        //添加资讯操作记录
        Random random = new Random();
        long articleActionId = random.nextLong();
        articleAction.setId(articleActionId);
        articleAction.setArticleId(id);
        articleAction.setOperatorUid(user.getId());
        articleAction.setOperatorUserName(user.getUserName());
        articleAction.setCreateTime(TimeUtil.getTime());
        articleAction.setOperatorType(5);

        articleMapper.updateArticle(constom);
    }

    @Override
    public void submitArticle(Long id, User user) {
        ArticleConstom constom = articleMapper.findById(id);
        constom.setSubmitUserName(user.getUserName());
        constom.setSubmitTime(TimeUtil.getTime());
        constom.setSubmitUid(user.getId());
        constom.setState(1);
        constom.setStateOrder(1);
        articleMapper.updateArticle(constom);
    }

    /**
     * 发布或者取消发布资讯
     * @param id
     * @param user
     */
    @Override
    public void publishArticle(Long id, User user) {
        ArticleConstom constom = articleMapper.findById(id);
        ArticleAction articleAction = new ArticleAction();

        //资讯的发布或者取消发布
        if(constom.getPublishState() == 0){//发布资讯
            constom.setState(4);
            constom.setStateOrder(4);
            constom.setPublishState(1);
            constom.setPublishTime(TimeUtil.getTime());
            constom.setPublishUid(user.getId());
            constom.setPublishUname(user.getUserName());
            articleMapper.updateArticle(constom);

            articleAction.setContent("发布了一条资讯");
        }else {//取消资讯的发布
            constom.setState(2);
            constom.setStateOrder(2);
            constom.setPublishState(0);
            constom.setPublishTime(TimeUtil.getTime());
            constom.setPublishUid(user.getId());
            constom.setPublishUname(user.getUserName());
            constom.setChannelTop(0);
            constom.setHomeTop(0);
            constom.setUnpublishTime(TimeUtil.getTime());
            constom.setUnpublishUid(user.getId());
            constom.setUnpublishUname(user.getName());
            articleMapper.updateArticle(constom);

            articleAction.setContent("取消一条资讯的发布");
        }

        //添加资讯操作记录
        Random random = new Random();
        long articleActionId = random.nextLong();
        articleAction.setId(articleActionId);
        articleAction.setArticleId(id);
        articleAction.setOperatorUid(user.getId());
        articleAction.setOperatorUserName(user.getUserName());
        articleAction.setCreateTime(TimeUtil.getTime());
        articleAction.setOperatorType(5);
        articleMapper.updateArticle(constom);
    }

    /**
     * 根据ID删除资讯
     * @param id
     */
    @Override
    public void deleteArticleById(Long id, User user) {
        //添加资讯操作记录
        ArticleAction articleAction = new ArticleAction();
        Random random = new Random();
        long articleActionId = random.nextLong();
        articleAction.setId(articleActionId);
        articleAction.setArticleId(id);
        articleAction.setOperatorUid(user.getId());
        articleAction.setOperatorUserName(user.getUserName());
        articleAction.setCreateTime(TimeUtil.getTime());
        articleAction.setContent("删除了一条资讯");
        articleAction.setOperatorType(4);

        articleMapper.deleteArticle(id);
        articleActionMapper.addArticleAction(articleAction);
    }

}
